package com.bulls.demo.hrmsdemo.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;

public class QueryManager {
	
	static Connection connection = null;
	static Statement stmt = null;
	static ResultSet rs = null;
	static ResultSetMetaData rsmd = null;
	static{
		try {
			Class.forName("org.postgresql.Driver");
			connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres", "REDDYS");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public static ArrayList<HashMap<String,Object>> executeQuery(String queryName){
		ArrayList<HashMap<String,Object>> resultList = new ArrayList<HashMap<String,Object>>();
		try {
			stmt = connection.createStatement();
			rs = stmt.executeQuery(queryName);
			rsmd = rs.getMetaData();
			while(rs.next()){
				HashMap hMap = new HashMap();
				System.out.println(rsmd.getColumnCount());
				for(int i=0;i<rsmd.getColumnCount();i++){
					System.out.println(rsmd.getColumnLabel(i+1));
					System.out.println(rs.getObject(i+1));
					hMap.put(rsmd.getColumnLabel(i+1), rs.getObject(i+1));
				}
				resultList.add(hMap);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return resultList;
	}

}
